package ru.kreda.webapp.client.ui;

import ru.kreda.webapp.client.AppConstants;

import com.extjs.gxt.ui.client.widget.Window;
import com.google.gwt.core.client.Scheduler;

/**
 * Базовый диалог для взаимодействия с пользователем.
 */
public abstract class WindowBasedUIDialog implements UIDialog {
	protected boolean enabled = AppConstants.UI_ENABLED;
	private Window window;
	private int width = 300;
	private int height = 200;
	private boolean autoHide = false;

	protected abstract Window create();

	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public void setAutoHide(boolean autoHide) {
		this.autoHide = autoHide;
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	public void show(int delayMs) {
		if (enabled) {
			Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {

				@Override
				public boolean execute() {
					show();
					return false;
				}

			}, delayMs);
		}
	}

	public void show() {
		if (enabled) {
			window = create();
			window.setAutoHide(autoHide);
			window.setSize(width, height);
			window.show();
		}
	}

	public void hide() {
		enabled = false;
		if (window != null) {
			window.hide();
		}
	}

	public void hide(int delayMs) {
		enabled = false;
		if (window != null) {
			Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {

				@Override
				public boolean execute() {
					hide();
					return false;
				}

			}, delayMs);
		}
	}

}
